/**
 * Author shuqi
 * Rev
 * Date: 2021-08-16 17:28
 * <p>
 * Copyright (C) 2021 Seeyon, Inc. All rights reserved.
 * <p>
 * This software is the proprietary information of Seeyon, Inc.
 * Use is subject to license terms.
 * @company seeyon.com
 * @since V5 V7.1
 * @author shuqi
 */
package com.lagou.config;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 解析server.xml
 * @description
 * @date 2021-08-16 17:28
 * @author Erekx
 */
public class ServerConfig {

    private static String SERVER = "server.xml";

    private static ServiceConfig serviceConfig;

    public static ServiceConfig getServiceConfig() {
        return serviceConfig;
    }

    public static ServiceConfig initConfig() throws DocumentException {
        //获取classpath
        String classpath = ServerConfig.class.getResource("/").getPath();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(classpath + File.separator + SERVER));
        Element root = document.getRootElement();
        Element service = root.element("Service");
        Element Connector = service.element("Connector");
        ServiceConfig serverConfig = new ServiceConfig();
        //设置监听端口
        Integer port = Integer.valueOf(Connector.attributeValue("port"));
        serverConfig.setPort(port);
        Element engine = service.element("Engine");
        Element host = engine.element("Host");
        //设置访问host
        String name = host.attributeValue("name");
        serverConfig.setHost(new Host(name,host.attributeValue("appBase")));
        //设置host->context映射
        List<Element> contexts = host.elements("Context");
        if(CollectionUtils.isNotEmpty(contexts)){
            Map<String,Context> cmap = Maps.newHashMap();
            contexts.forEach(context -> {
               String path = context.attributeValue("path");
               String docBase = context.attributeValue("docBase");
               cmap.put(path,new Context(path, docBase));
            });
            Map<String, Map<String,Context>> config = Maps.newHashMap();
            config.put(name + ":" + port,cmap);
            serverConfig.setConfig(config);
        }
        serviceConfig = serverConfig;
        return serverConfig;
    }



    @Data
    public static class ServiceConfig{
        //监听端口
        private Integer port;
        private Host host;
        //host->context映射
        private Map<String,Map<String,Context>> config;
    }

    @Data
    @AllArgsConstructor
    public static class Host{
        //域名
        private String domain;
        //路径
        private String appBase;
    }

    @Data
    @AllArgsConstructor
    public static class Context{
        //部署名
        private String applicationContext;
        //项目路径
        private String docBase;
    }
}
